package com.siyao.redvip.ui.activity;

import com.siyao.redvip.R;
import java.util.HashMap;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import android.widget.Button;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;
import com.android.volley.VolleyError;
import com.siyao.redvip.RedApplication;
import com.siyao.redvip.ui.loadpage.LoadPageActivity;
import com.siyao.redvip.utils.ToastUtils;
import com.siyao.redvip.utils.Track;
import com.google.gson.reflect.TypeToken;
import com.siyao.redvip.bean.ResponseBean;
import com.siyao.redvip.volley.NetRequest;
import com.siyao.redvip.network.NetConstans;
import com.siyao.redvip.components.base.BaseActivity;
import com.siyao.redvip.utils.SharedPreferencesUtils;
import com.siyao.redvip.volley.VolleyErrorListener;
import com.siyao.redvip.volley.VolleySuccessListener;


/**
 * 登陆 LoginActivity
 * 
 * @author wangxuebin
 * 
 */
public class LoginActivity extends BaseActivity {
	
	private boolean isFrist;//是否是第一次

	private EditText etAccount;// 用户名

	private EditText etPasswrod;// 密码

	private Button btnLogin;// 登陆按钮

	private TextView tvFrogetPassword;

	private TextView tvRegisterAccount;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	
	@Override
	protected void beforeInit() {
		//首先如果是第一次加载应用要进入loadpage页面
		isFrist  = SharedPreferencesUtils.getIsFrist(this);
		if(isFrist){
			Intent intents =new Intent(LoginActivity.this, LoadPageActivity.class); 
			startActivity(intents);
			finish();
		}
		
		//如果xml文件里有用户名跟密码就直接登录
		if (!TextUtils.isEmpty(SharedPreferencesUtils.getAccount(this)) && SharedPreferencesUtils.isSavePasswd(this)
				&& !TextUtils.isEmpty(SharedPreferencesUtils.getPasswd(this)) ) {
			String account =SharedPreferencesUtils.getAccount(this);
			String passwd =SharedPreferencesUtils.getPasswd(this);
			doLoginAction(account, passwd);
		}
	}

	@Override
	public void onClick(View v) {
		int viewId = v.getId();
		switch (viewId) {
		case R.id.btnLogin:
			doLogin();
			break;
		case R.id.tvFrogetPassword:
			break;
		case R.id.tvRegisterAccount:

			break;
		default:
			break;
		}

	}

	// 登陆操作
	private void doLogin() {
		String account = etAccount.getText().toString();// 用户名
		String passwd = etPasswrod.getText().toString();// 密码
		if (account == null && TextUtils.isEmpty(account)) {
			Toast.makeText(this, getString(R.string.input_user_name),
					Toast.LENGTH_LONG).show();
			return;
		}
		if (passwd == null && TextUtils.isEmpty(passwd)) {
			Toast.makeText(this, getString(R.string.input_not_null),
					Toast.LENGTH_LONG).show();
			return;
		}

		doLoginAction(account, passwd);
	}

	@Override
	protected void setContentView() {
		setContentView(R.layout.activity_login);
	}

	@Override
	protected void initComponent() {
		etAccount = (EditText) findViewById(R.id.etAccount);
		etPasswrod = (EditText) findViewById(R.id.etPasswrod);
		btnLogin = (Button) findViewById(R.id.btnLogin);
		tvFrogetPassword = (TextView) findViewById(R.id.tvFrogetPassword);
		tvRegisterAccount = (TextView) findViewById(R.id.tvRegisterAccount);

		btnLogin.setOnClickListener(this);
		tvFrogetPassword.setOnClickListener(this);
		tvRegisterAccount.setOnClickListener(this);
	}

	@Override
	protected void initData() {
		if (!TextUtils.isEmpty(SharedPreferencesUtils.getAccount(this))) {
			etAccount.setText(SharedPreferencesUtils.getAccount(this));
		}
		if (SharedPreferencesUtils.isSavePasswd(this)
				&& !TextUtils.isEmpty(SharedPreferencesUtils.getPasswd(this))) {
			etPasswrod.setText(SharedPreferencesUtils.getPasswd(this));
		}
	}

	// 登陆action
	private void doLoginAction(final String account, final String passwd) {
		showPd();
		// 1 http请求成功之后
		// 2 保存用户名跟密码
		String loginReqUrl = NetConstans.LOGIN_URL;

		HashMap<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("loginName", account);
		paramMap.put("password", passwd);
		NetRequest netGet = new NetRequest(this);
		netGet.doPostRequest(loginReqUrl, paramMap,
				new VolleySuccessListener() {
					@Override
					public void onResponse(String response) {
						super.onResponse(response);
						Track.i(response);
						if (netBean.getStatus() == 1) {
							SharedPreferencesUtils.savePasswd(
									LoginActivity.this, passwd);
							SharedPreferencesUtils.saveAccount(
									LoginActivity.this, account);
							SharedPreferencesUtils.setSavePasswd(
									LoginActivity.this, true);
							ResponseBean userbean = gson.fromJson(response, new TypeToken<ResponseBean>() {}.getType());
							RedApplication.getInstance().setUser(userbean.getData());
							startActivity(new Intent(LoginActivity.this,
									RedvipMainActivity.class));
							ToastUtils.makeMesageShort(LoginActivity.this,
									getString(R.string.server_succes));
							finish();

						} else {
							ToastUtils.makeMesageShort(LoginActivity.this,netBean.getMessage());
						}
						dismissPd();
					}
				}, new VolleyErrorListener() {
					@Override
					public void onErrorResponse(VolleyError error) {
						dismissPd();
						ToastUtils.makeMesageLong(LoginActivity.this,
								getString(R.string.server_error));
					}
				});
	}
	
}